package com.lepu.app.fun.tool.szqx.adapter;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.DateUtil;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.szqx.bean.BeanToolGrow;

@SuppressLint("InflateParams")
public class GrowDataAdapter extends BaseAdapter {

	public LayoutInflater layoutInflater = null;
	public Context context = null;
	public ArrayList<BeanToolGrow> dataList;
	public int type;
	public GrowDataAdapter(Context context , ArrayList<BeanToolGrow> dataList, int type) {
		this.context = context;
		this.dataList = dataList;
		this.type = type;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		final ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.grow_data_item, null);

			holder = new ViewHolder();
			TextView old = (TextView) convertView.findViewById(R.id.grow_item_old);
			TextView date = (TextView) convertView.findViewById(R.id.grow_item_date);
			TextView target = (TextView) convertView.findViewById(R.id.grow_item_target);

			holder.old = old;
			holder.date = date;
			holder.target = target;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
	
		BeanToolGrow bean = dataList.get(position);
		String babyDate = UserConfig.getConfigString(context, Const.CONFIG_APP_BABY_DATE, "");
		
		holder.old.setText(DateUtil.getToDateYear(bean.time, DateUtil.getLongTime(babyDate))+"岁");
		holder.date.setText(DateUtil.getToDateMonth(bean.time, DateUtil.getLongTime(babyDate)));
		if(type == 1){
			holder.target.setText(bean.weight+" / kg");
		}else if(type == 2){
			holder.target.setText(bean.height+" / cm");
		}else if(type == 3){
			holder.target.setText(bean.head+" / cm");
		}
		

		if(position > 0 && DateUtil.getToDateYear(dataList.get(position-1).time,DateUtil.getLongTime(babyDate)) == DateUtil.getToDateYear(bean.time,DateUtil.getLongTime(babyDate)) ){
			holder.old.setVisibility(View.GONE);
		}else{
			holder.old.setVisibility(View.VISIBLE);
		}
		
		return convertView;
	}
	
	static class ViewHolder {
		TextView old;
		TextView date;
		TextView target;
	}
}
